<template>
  <div class="app-container">
    <el-card class="box-card" v-if="pageType == 1">
      <!-- 搜索区域 -->
      <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="100px">
        <el-form-item label="出库单号" prop="inOrOutOrderNumber">
          <el-input v-model="queryParams.inOrOutOrderNumber" placeholder="请输入出库单号" clearable size="small" />
        </el-form-item>
        <!-- <el-form-item label="退货单号" prop="returnNo">
          <el-input v-model="queryParams.returnNo" placeholder="请输入退货单号" clearable size="small" />
        </el-form-item> -->
        <el-form-item label="供应商" prop="supplier">
          <el-select v-model="queryParams.merId" placeholder="请选择供应商" clearable size="small">
            <el-option v-for="item in supplierList" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="出库状态" prop="status">
         <el-select v-model="queryParams.reviewStatus" placeholder="请选择出库状态" clearable size="small">
            <el-option label="待审核" value="0" />
            <el-option label="已通过" value="1" />
            <el-option label="已驳回" value="2" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <!-- 操作按钮区域 -->
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
        </el-col>
      </el-row>

      <!-- 数据表格 -->
      <el-table v-loading="loading" :data="tableData">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="出库单号" align="center" prop="inOrOutOrderNumber" />
        <!-- <el-table-column label="退货单号" align="center" prop="returnNo" /> -->
        <el-table-column label="供应商" align="center" prop="transferOutRoomWarehouseName" />
        <el-table-column label="出库仓库" align="center" prop="transferInRoomWarehouseName" />
        <el-table-column label="出库状态" align="center" prop="inOrOutWarehouseStatus">
          <template slot-scope="scope">
                       <el-tag :type="getStatusType(scope.row.inOrOutWarehouseStatus)">{{ getStatusText(scope.row.inOrOutWarehouseType,scope.row.inOrOutWarehouseStatus) }}</el-tag>

          </template>
        </el-table-column>
        <el-table-column label="创建时间" align="center" prop="createTime" width="180" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button type="text" size="mini" @click="handleDetail(scope.row)">详情</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>
    <el-card class="box-card" v-else-if="pageType == 3">
      <select-product
        @handleBack="handleBack"
        :defaultTransferInType="defaultTransferInType"
        sendType="退货出库"
        :defaultTransferOutType="defaultTransferOutType"
      ></select-product>
    </el-card>
    <el-card class="box-card" v-else-if="pageType == 2">
      <purchase-receipt-detail
        :detail-data="detailData"
        :detail-row="detailRow"
        @back="handleBack"
        @approve="handleApprove"
      />
    </el-card>
  </div>
</template>

<script>
import PurchaseReceiptDetail from './detail.vue';
import { storeHouseMixin } from '@/mixins/storeHouse';
import { getPurchaseInWarehouseFormList, getPurchaseInWarehouseFormInfo,getInventoryTransferInOutList } from '@/api/storehouse';
import { merchantListApi } from '@/api/merchant';

export default {
  name: 'ReturnIssue',
  mixins: [storeHouseMixin],
  components: {
    PurchaseReceiptDetail,
  },
  data() {
    return {
      // 遮罩层
      loading: false,
      // 总条数
      total: 0,
      // 表格数据
      tableData: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        issueNo: undefined,
        returnNo: undefined,
        supplier: undefined,
        status: undefined,
      },
      defaultTransferOutType: 0,
      defaultTransferInType: 3, // 0: 供/ 0: 供应商 1: 门仓 2: 门店 3：仓库
      supplierList: [],
    };
  },
  created() {
    this.getList();
    this.getSupplierList()

  },
  methods: {
     getSupplierList() {
      merchantListApi({ page: 1, limit: 999 }).then((res) => {
        this.supplierList = res.list;
      });
    },
    /** 查看详情按钮操作 */
    handleDetail(row) {
      console.log(row);
      this.detailRow = row;
      // TODO: 实现查看详情逻辑
      getPurchaseInWarehouseFormInfo({ id: row.id }).then((res) => {
        console.log(res);
        this.detailData = res;
      });
      this.pageType = 2;
    },
    /** 状态显示 */
    getStatusType(status) {
      if (status == '0') return 'warning';
      if (status == '1') return 'success';
      if (status == '2') return 'danger';
      return '';
    },
   /** 获取状态文本 */
    getStatusText(type,status) {
      // 0:待出库(待入库) 1:已出库(已入库) 2:已取消
      const statusMap = {
        1: {
          0: "待出库",
          1: "已出库",
          2: "已取消",
        },
        2: {
          0: "待入库",
          1: "已入库",
          2: "已取消",
        },
      };
      return (
        statusMap[type][
          status
        ] || "未知状态"
      );
    },

    /** 查询列表 */
    /** 查询列表 */
    getList() {
      let params = {
        inOrOutWarehouseType: 1,
        page: this.queryParams.page,
        limit: this.queryParams.limit,
        transferInRoomWarehouseId: this.queryParams.merId,
        transferOrderNumber: this.queryParams.inOrOutOrderNumber,
        operateType: 2,
        inOrOutWarehouseStatus: this.queryParams.reviewStatus,
      };
      getInventoryTransferInOutList(params).then((res) => {
        this.tableData = res.list;
        this.total = res.total;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm');
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      // this.isAdd = true
      this.pageType = 3;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      // TODO: 实现修改逻辑
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      // TODO: 实现删除逻辑
    },
  },
};
</script>

<style scoped>
.app-container {
  padding: 20px;
}
</style>
